/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los terminos
 * de la licencia "GNU General Public License" publicada por la Fundacion "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser util, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacion.
 */
package adalid.core;

import adalid.core.annotations.ReportOperationClass;
import adalid.core.enums.ReportQueryType;
import adalid.core.enums.SortOption;
import java.lang.annotation.Annotation;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 * @author Jorge Campins
 */
public abstract class ReportOperation extends Operation {

    private boolean _annotatedWithReportOperationClass;

    private String _reportName;

    private ReportQueryType _queryType;

    private int _rowsLimit;

    private SortOption _sortOption;

    /**
     * @return true if annotated with ReportOperationClass; false otherwise
     */
    public boolean isAnnotatedWithReportOperationClass() {
        return _annotatedWithReportOperationClass;
    }

    /**
     * @return the report name
     */
    public String getReportName() {
        return _reportName;
    }

    /**
     * @return the query type
     */
    public ReportQueryType getQueryType() {
        return _queryType;
    }

    /**
     * @return the rows limit
     */
    public int getRowsLimit() {
        return _rowsLimit;
    }

    /**
     * @return the sort option
     */
    public SortOption getSortOption() {
        return _sortOption;
    }

    // <editor-fold defaultstate="collapsed" desc="annotate">
    @Override
    void initializeAnnotations() {
        super.initializeAnnotations();
        _annotatedWithReportOperationClass = false;
        _reportName = getName();
        _queryType = ReportQueryType.DYNAMIC;
        _rowsLimit = Constants.DEFAULT_REPORT_ROWS_LIMIT;
        _sortOption = SortOption.ASC;
    }

    @Override
    void annotate(Class<?> type) {
        super.annotate(type);
        if (type != null) {
            annotateReportOperationClass(type);
        }
    }

    @Override
    protected List<Class<? extends Annotation>> getValidTypeAnnotations() {
        List<Class<? extends Annotation>> valid = super.getValidTypeAnnotations();
        valid.add(ReportOperationClass.class);
        return valid;
    }

    private void annotateReportOperationClass(Class<?> type) {
        Class<?> annotatedClass = XS1.getAnnotatedClass(type, ReportOperationClass.class);
        if (annotatedClass != null) {
            ReportOperationClass annotation = annotatedClass.getAnnotation(ReportOperationClass.class);
            if (annotation != null) {
                String name = annotation.name();
                if (StringUtils.isNotBlank(name)) {
                    _reportName = name;
                }
                _queryType = annotation.type();
                _rowsLimit = annotation.rowsLimit();
                _sortOption = annotation.sortOption();
                _annotatedWithReportOperationClass = true;
            }
        }
    }
    // </editor-fold>

}
